package comp.display 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import util.ElemLib;
	
	/**
	 * ...
	 * @author Qi Donghui
	 */
	public class PicContainer extends Sprite 
	{
		private var _source:String;
		
		private var _picLoader:Loader;
		
		private var _pic:Bitmap;
		
		private var _msk:Sprite;
		
		public function PicContainer() 
		{
			
		}
		
		private function loadPic():void
		{	
			if (ElemLib.getInstance().getElemByName(_source) != null) picReady();
			else ElemLib.getInstance().addElem(_source, _source, picReady);
		}
		
		private function picReady():void
		{
			var ba:ByteArray = ElemLib.getInstance().getElemByName(_source) as ByteArray;
			_picLoader = new Loader();
			_picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPicLoaded);
			_picLoader.loadBytes(ba);
		}
		
		private function onPicLoaded(evt:Event):void
		{
			_pic = _picLoader.content as Bitmap;
			
			this.graphics.clear();
			this.graphics.beginFill(0xbbbbbb)
			this.graphics.drawRect(0, 0, _pic.width, _pic.height);
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRect(1, 1, _pic.width-2, _pic.height-2);
			this.graphics.endFill();
			
			
			addChild(_pic);
			_msk = new Sprite();
			_msk.graphics.beginFill(0x000000);
			_msk.graphics.drawRect(2, 2, _pic.width - 4, _pic.height - 4);
			_msk.graphics.endFill();
			addChild(_msk);
			_pic.mask = _msk;
			
			dispatchEvent(new Event(Event.COMPLETE));
		}		
		
		public function get source():String 
		{
			return _source;
		}
		
		public function set source(value:String):void 
		{
			_source = value;
			if (value == null) return;
			if (value == "") return;
			loadPic();
		}
		
	}

}